/* 
 * Copyright [2018] [Alex/libo(liboware@gmail.com)]
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.hyts.infodb.platform;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;


/**
 * @author LiBo/Alex
 * @version V1.0 
 */
//@EnableApiDoc(basePackage = "com.hyts.infodb")
//@EnableCustomMetrics
@SpringBootApplication(exclude=DataSourceAutoConfiguration.class)
//@Import({ShiroConfiguration.class,InfodbConfiguration.class, SpringContextConfiguration.class,RestRemplateConfiguration.class,FastJsonConfiguration.class})
public class PlatformApplication implements WebServerFactoryCustomizer<TomcatServletWebServerFactory>{


    public static void main(String[] args) {
        SpringApplication.run(PlatformApplication.class, args);
    }

    /* (非 Javadoc)
     * <b>TODO(方法功能的描述)-  customize </b>
     * <p>TODO(这里用一句话描述这个方法的作用)</p>
     * @param factory
     * @see org.springframework.boot.web.server.WebServerFactoryCustomizer#customize(org.springframework.boot.web.server.WebServerFactory)  
     * @exception
     */ 
    @Override
    public void customize(TomcatServletWebServerFactory factory) {
        factory.setPort(8882);
    }

    @Bean
    public FilterRegistrationBean corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        CorsConfiguration config = new CorsConfiguration();
        config.setAllowCredentials(true);   config.addAllowedOrigin("http://localhost:8882");
        config.addAllowedOrigin("null");
        config.addAllowedHeader("*");
        config.addAllowedMethod("*");
        source.registerCorsConfiguration("/**", config); // CORS 配置对所有接口都有效
        FilterRegistrationBean bean = new FilterRegistrationBean(new CorsFilter(source));
        bean.setOrder(0);
        return bean;
    }
    
}
